package com.qef.com.mmovie.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qef.com.mmovie.R;


/**
 * 2016 逛街购（北京）网络科技有限公司，版权所有
 * guangjiegou - Android客户端
 * Content:自定义进度框
 * Created by fanenqian on 2017/4/26 10:33
 */
public class ProgressDialogF extends Dialog {

    private final TextView mMessage;

    public ProgressDialogF(Context context) {
        super(context, R.style.Theme_Dialog);
        // View contentView
        // LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
        setContentView(R.layout.dialog_progress);
        mMessage = (TextView) findViewById(R.id.textview_message);
        // setWindowWidth(0.9);
        setWindowWidth(-1);
    }

    public ProgressDialogF(Context context, int type, double percent) {
        super(context, R.style.Theme_Dialog);
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.dialog_progress, null);
        setContentView(contentView);
        mMessage = (TextView) findViewById(R.id.textview_message);
        // setWindowWidth(0.9);
        setWindowWidth(percent);
        if (type == 1) {
            ProgressBar prbar = (ProgressBar) findViewById(R.id.progressbar);
            contentView.findViewById(R.id.progressbar_content)
                    .setBackgroundResource(R.drawable.common_toagst_bg);
            prbar.setIndeterminateDrawable(context.getResources().getDrawable(
                    R.drawable.loading_progress));
        }
    }

    /**
     *
     */
    public void setWindowWidth(double percent) {
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        Display play = window.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        play.getMetrics(metrics);
        window.setLayout(percent == -1 ? WindowManager.LayoutParams.MATCH_PARENT
                        : (int) (metrics.widthPixels * percent),
                percent == -1 ? WindowManager.LayoutParams.MATCH_PARENT
                        : (int) (metrics.widthPixels * percent));

    }

    public void setMessage(CharSequence message) {
        if (!TextUtils.isEmpty(message)) {
            mMessage.setVisibility(View.VISIBLE);
        }
        mMessage.setText(message);
    }

    public void setMessage(int resId) {
        mMessage.setVisibility(View.VISIBLE);
        mMessage.setText(resId);
    }
}
